package ar.uade.edu.fain.ad.client.delegate.rmi;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;
import java.util.Set;

import ar.uade.edu.fain.ad.client.delegate.CajaServiceDelegate;
import ar.uade.edu.fain.ad.client.delegate.rmi.util.ServiceLocator;
import ar.uade.edu.fain.ad.client.exception.UnavailableServerException;
import ar.uade.edu.fain.ad.common.exception.FacturaYaPagadaException;
import ar.uade.edu.fain.ad.common.exception.PedidoFacturadoException;
import ar.uade.edu.fain.ad.common.exception.PedidoPendienteException;
import ar.uade.edu.fain.ad.common.remote.CajaServicioRemoto;
import ar.uade.edu.fain.ad.common.transfer.CajaVO;
import ar.uade.edu.fain.ad.common.transfer.CierreCajaVO;
import ar.uade.edu.fain.ad.common.transfer.FacturaVO;
import ar.uade.edu.fain.ad.common.transfer.LocalVO;
import ar.uade.edu.fain.ad.common.transfer.MedioPagoVO;
import ar.uade.edu.fain.ad.common.transfer.PedidoVO;

public class CajaSRVD implements CajaServiceDelegate {

	private ServiceLocator<CajaServicioRemoto> locator;

	public CajaSRVD(ServiceLocator<CajaServicioRemoto> locator) {
		super();
		this.locator = locator;
	}

	public ServiceLocator<CajaServicioRemoto> getLocator() {
		return locator;
	}

	public CajaServicioRemoto getRemoteService()
			throws UnavailableServerException {
		return getLocator().getRemoteService();
	}

	@Override
	public List<MedioPagoVO> buscarMediosPagoHabilitados() {
		try {
			return getRemoteService().buscarMediosPagoHabilitados();
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}
	
	@Override
	public List<PedidoVO> buscarPedidosSinFactura(final LocalVO localVO) {
		try {
			return getRemoteService().buscarPedidosSinFactura(localVO);
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}

	@Override
	public FacturaVO facturar(CajaVO cajaVO, Set<PedidoVO> pedidoVOs)
			throws PedidoFacturadoException {
		try {
			return getRemoteService().facturar(cajaVO, pedidoVOs);
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}

	@Override
	public void registrarPago(FacturaVO facturaVO, MedioPagoVO medioPagoVO)
			throws FacturaYaPagadaException {
		try {
			getRemoteService().registrarPago(facturaVO, medioPagoVO);
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}

	@Override
	public void calcularComisiones(LocalVO localVO, Date fecha) 
			throws PedidoPendienteException{
		try {
			getRemoteService().calcularComisiones(localVO, fecha);
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}

	@Override
	public CierreCajaVO prepararCierre(CajaVO cajaVO)
			throws PedidoPendienteException {
		try {
			return getRemoteService().prepararCierre(cajaVO);
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}

	@Override
	public void confirmarCierre(CierreCajaVO cierreCajaVO)
			throws PedidoPendienteException {
		try {
			getRemoteService().confirmarCierre(cierreCajaVO);
		} catch (RemoteException e) {
			throw new UnavailableServerException(
					"Error ejecutando remotamente.", e);
		} catch (UnavailableServerException e) {
			throw e;
		}
	}

}
